/**
 * 
 */
package jnzb.model.event;

import java.util.LinkedList;

import javax.swing.table.AbstractTableModel;

/**
 * @author AA1
 * 
 */
public class EventTableModel extends AbstractTableModel {

	private static final String[] COLUMNS = new String[] { "Time", "Type",
			"Event" };
	private LinkedList<Event> events = new LinkedList<Event>();

	/**
	 * Maximum number of events in the list
	 */
	private static final int MAX_SIZE = 100;

	/**
	 * 3 columns - Time, Type, Event
	 * 
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return 3;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		return events == null ? 0 : events.size();
	}

	/**
	 * @see javax.swing.table.AbstractTableModel#getColumnName(int)
	 */
	@Override
	public String getColumnName(int column) {
		return COLUMNS[column];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		String value;
		switch (columnIndex) {
		case 0:
			value = events.get(rowIndex).getTime();
			break;
		case 1:
			value = events.get(rowIndex).getType().toString();
			break;
		case 2:
			value = events.get(rowIndex).getDescription();
			break;
		default:
			value = null;
		}
		return value;
	}

	/**
	 * Add an event to the model
	 * @param event
	 */
	public void addEvent(Event event) {
		this.events.addFirst(event);
		checkSize();
		this.fireTableDataChanged();
	}

	/**
	 * Recursively calls itself to limit the size of the list to the max_size
	 */
	private void checkSize() {
		if (events.size() > MAX_SIZE) {
			events.removeLast();
			checkSize();
		}
	}

	public boolean isEmpty() {
		return this.events.isEmpty();
	}

	public String getFirst() {
		return this.events.getFirst().getDescription();
	}
}
